<?php
namespace Home\Controller;
use Home\Controller\IndexController;
class ClientContactController extends IndexController 
{
    public function add()
    {
        $id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Home/ClientContact');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?id='.I('post.client_id')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        $this->assign('id',$id);
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
        
    	if(IS_POST)
    	{
            $gid = I('post.client_id');
        
    		$model = D('Home/ClientContact');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('id' =>$gid )));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('ClientContact');
    	$data = $model->find($id);
    	$this->assign('data', $data);

		$this->display();
    }
    public function delete()
    {
    	$model = D('Home/ClientContact');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst?id='.I('get.gid')));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
        $id=I('get.id');
        $this->assign('id',$id);
    	$model = D('Home/ClientContact');
    	$data = $model->search();
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));
        
                      //取出客户名字
        $cModel=M('Client');
        $name=$cModel->field('name')->where(array('id'=>array('eq',$id)))->find();
        $this->assign($name);
    	$this->display();
    }
}